<template>
  <div>
    <el-button
      type="primary"
      :icon="Plus"
      style="margin-bottom: 15px"
      @click="addSpuInfo"
      :disabled="!categoryStore.category3Id"
      >添加SPU</el-button
    >
    <el-table border style="margin-bottom: 15px" :data="spuList">
      <el-table-column
        type="index"
        label="序号"
        width="80px"
        aligin="center"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row, $index }">
          <el-button
            size="small"
            type="primary"
            title="添加SKU"
            :icon="Plus"
            @click="addSpu(row)"
          ></el-button>
          <el-button
            size="small"
            type="primary"
            title="修改SKU"
            :icon="Edit"
            @click="editSpu(row)"
          ></el-button>
          <el-button
            size="small"
            type="info"
            title="查看SKU列表"
            :icon="InfoFilled"
            @click="showSkuList(row)"
          ></el-button>
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            :icon="InfoFilled"
            icon-color="orange"
            title="确定删除此条SPU?"
            @confirm="confirmDelSpu(row, $index)"
          >
            <template #reference>
              <el-button
                size="small"
                type="danger"
                title="删除SKU"
                :icon="Delete"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog v-model="dialogTableVisible" :title="`${spuName} => SKU列表`">
      <el-table :data="skuList">
        <el-table-column property="skuName" label="名称" width="200" />
        <el-table-column property="price" label="价格(元)" width="150" />
        <el-table-column property="weight" label="重量(KG)" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <el-image
              :src="row.skuDefaultImg"
              :alt="row.skuDesc"
              style="width: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script lang="ts">
export default {
  name: 'SpuList',
}
</script>
<script lang="ts" setup>
import { ref, watch, onUnmounted, onMounted } from 'vue'
import { Plus, Edit, Delete, InfoFilled } from '@element-plus/icons-vue'
import { useCategoryStore } from '@/stores/category'
import { reqGetSpuListData, reqDelOneSpu } from '@/api/product/spu'
import { reqGetSkuListData } from '@/api/product/sku'
import { ElMessage } from 'element-plus'

const categoryStore = useCategoryStore()
const currentPage = ref<number>(1)
const pageSize = ref<number>(3)
const total = ref<number>(0)
const spuList = ref([])
const emits = defineEmits(['getScene', 'getSpuInfo'])
const dialogTableVisible = ref(false)
const spuName = ref('')
const skuList = ref([])

// 分页相关的两个事件
// 1.封装获取spu数据列表请求
const getSpuListData = async () => {
  const result = await reqGetSpuListData(
    currentPage.value,
    pageSize.value,
    categoryStore.category3Id
  )
  spuList.value = result.records
  total.value = result.total
}

//2.监听category3Id数据变化
watch(
  () => categoryStore.category3Id,
  async (category3Id) => {
    if (!category3Id) return
    console.log(categoryStore.category3Id)
    getSpuListData()
  })

// 3. 更改每页显示条数时触发的事件处理程序
const handleSizeChange = (val: number) => {
  // val就是每页显示的条数 其实也相当于是pageSize
  pageSize.value = val
  currentPage.value = 1
  getSpuListData()
}

// 4. 根据当前页码进行切换时触发的函数
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`)
  currentPage.value = val
  getSpuListData()
}

// 5.删除spu事件函数
const confirmDelSpu = async (row) => {
  await reqDelOneSpu(row.id)
  ElMessage.success('参数成功')
  getSpuListData()
}

//6.编辑spu事件函数
const editSpu = (row) => {
  emits('getSpuInfo', row)
  emits('getScene', 2)
}

//6.添加spu
const addSpuInfo = () => {
  emits('getScene', 2)
  emits('getSpuInfo', {})
}

// 7.添加sku
const addSpu = (row) => {
  emits('getScene', 3)
  emits('getSpuInfo', row)
}

// 8.查看sku列表
const showSkuList = async (row) => {
  dialogTableVisible.value = true
  spuName.value = row.spuName
  const result = await reqGetSkuListData(row.id)
  skuList.value = result
}
</script>

<style></style>
